/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package com.unwiredgrain.modeling.sscm.impl;

import com.unwiredgrain.modeling.sscm.Socket;

import com.unwiredgrain.modeling.sscm.InterfaceTemplateInstantiation;
import com.unwiredgrain.modeling.sscm.InnerConnector;
import com.unwiredgrain.modeling.sscm.Connector;
import com.unwiredgrain.modeling.sscm.Interface;
import com.unwiredgrain.modeling.sscm.Port;
import com.unwiredgrain.modeling.sscm.PortDirection;
import com.unwiredgrain.modeling.sscm.Module;
import com.unwiredgrain.modeling.sscm.SSCMPackage;

import java.util.Collection;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;

import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.InternalEList;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Compartment</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link com.unwiredgrain.modeling.sscm.impl.SocketImpl#getInterface <em>Interface</em>}</li>
 *   <li>{@link com.unwiredgrain.modeling.sscm.impl.SocketImpl#getInterfaceTemplateInstantiation <em>Interface Template Instantiation</em>}</li>
 *   <li>{@link com.unwiredgrain.modeling.sscm.impl.SocketImpl#getModule <em>Module</em>}</li>
 *   <li>{@link com.unwiredgrain.modeling.sscm.impl.SocketImpl#getInnerConnectors <em>Inner Connectors</em>}</li>
 *   <li>{@link com.unwiredgrain.modeling.sscm.impl.SocketImpl#isEnabled <em>Enabled</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class SocketImpl extends NamedElementImpl implements Socket {
	/**
	 * The cached value of the '{@link #getInterfaceTemplateInstantiation() <em>Interface Template Instantiation</em>}' containment reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getInterfaceTemplateInstantiation()
	 * @generated
	 * @ordered
	 */
	protected InterfaceTemplateInstantiation interfaceTemplateInstantiation;

	/**
	 * The cached value of the '{@link #getModule() <em>Module</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getModule()
	 * @generated
	 * @ordered
	 */
	protected Module module;

	/**
	 * The cached value of the '{@link #getInnerConnectors() <em>Inner Connectors</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getInnerConnectors()
	 * @generated
	 * @ordered
	 */
	protected EList<InnerConnector> innerConnectors;

	/**
	 * The default value of the '{@link #isEnabled() <em>Enabled</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isEnabled()
	 * @generated
	 * @ordered
	 */
	protected static final boolean ENABLED_EDEFAULT = true;

	/**
	 * The cached value of the '{@link #isEnabled() <em>Enabled</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isEnabled()
	 * @generated
	 * @ordered
	 */
	protected boolean enabled = ENABLED_EDEFAULT;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected SocketImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return SSCMPackage.Literals.SOCKET;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Interface getInterface() {
		Interface interface_ = basicGetInterface();
		return interface_ != null && interface_.eIsProxy() ? (Interface)eResolveProxy((InternalEObject)interface_) : interface_;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public Interface basicGetInterface() {
		// DONE: implement this method to return the 'Facade' reference
		// -> do not perform proxy resolution
		// Ensure that you remove @generated or mark it @generated NOT
		// throw new UnsupportedOperationException();
		if (getInterfaceTemplateInstantiation() == null) {
			return null;
		}
		
		return getInterfaceTemplateInstantiation().getInterface();
	}

	/**
	 * <!-- begin-user-doc -->
	 * This is a non-transient, volatile method. So it's safe to create objects here.
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public void setInterface(Interface newFacade) {
		// DONE: implement this method to set the 'Facade' reference
		// Ensure that you remove @generated or mark it @generated NOT
		// throw new UnsupportedOperationException();
		Interface oldFacade = getInterface();
		
		if (getInterfaceTemplateInstantiation() == null) {
			InterfaceTemplateInstantiation  fti = SSCMFactoryImpl.eINSTANCE.createInterfaceTemplateInstantiation();
			setInterfaceTemplateInstantiation(fti);
		}
		
		getInterfaceTemplateInstantiation().setInterface(newFacade);
		
		getInnerConnectors().clear();
		
		if (newFacade == null) {
			setInterfaceTemplateInstantiation(null);
			return;
		}
		
		for (Port port : newFacade.getPorts()) {
			
			// Connector conn;
			/**
			if (facet.getDirection() == FacetDirection.PROVIDES) {
				if (facet.isMultiple() == false) {
					conn = SSCMFactoryImpl.eINSTANCE.createSingleProvider();
				}
				else if (facet.isAutosize()) {
					conn = SSCMFactoryImpl.eINSTANCE.createAutoSizeVectorProvider();
				}
				else {
					conn = SSCMFactoryImpl.eINSTANCE.createFixedSizeVectorProvider();
				}
			} 
			else if (facet.getDirection() == FacetDirection.REQUIRES) {
				if (facet.isMultiple() == false) {
					conn = SSCMFactoryImpl.eINSTANCE.createSingleUser();
				}
				else if (facet.isAutosize()) {
					conn = SSCMFactoryImpl.eINSTANCE.createAutoSizeVectorUser();
				}
				else {
					conn = SSCMFactoryImpl.eINSTANCE.createFixedSizeVectorUser();
				}
			}
			else {
				throw new UnsupportedOperationException();
			}
			**/
			// conn.setFacet(facet);
			// getConnectors().add(conn);
		}
		
		if (eNotificationRequired()) {
			eNotify(new ENotificationImpl(this, Notification.SET, SSCMPackage.SOCKET__INTERFACE, oldFacade, newFacade));
		}
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public boolean isEnabled() {
		return enabled;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setEnabled(boolean newEnabled) {
		boolean oldEnabled = enabled;
		enabled = newEnabled;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, SSCMPackage.SOCKET__ENABLED, oldEnabled, enabled));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public InterfaceTemplateInstantiation getInterfaceTemplateInstantiation() {
		return interfaceTemplateInstantiation;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetInterfaceTemplateInstantiation(InterfaceTemplateInstantiation newInterfaceTemplateInstantiation, NotificationChain msgs) {
		InterfaceTemplateInstantiation oldInterfaceTemplateInstantiation = interfaceTemplateInstantiation;
		interfaceTemplateInstantiation = newInterfaceTemplateInstantiation;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, SSCMPackage.SOCKET__INTERFACE_TEMPLATE_INSTANTIATION, oldInterfaceTemplateInstantiation, newInterfaceTemplateInstantiation);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setInterfaceTemplateInstantiation(InterfaceTemplateInstantiation newInterfaceTemplateInstantiation) {
		if (newInterfaceTemplateInstantiation != interfaceTemplateInstantiation) {
			NotificationChain msgs = null;
			if (interfaceTemplateInstantiation != null)
				msgs = ((InternalEObject)interfaceTemplateInstantiation).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - SSCMPackage.SOCKET__INTERFACE_TEMPLATE_INSTANTIATION, null, msgs);
			if (newInterfaceTemplateInstantiation != null)
				msgs = ((InternalEObject)newInterfaceTemplateInstantiation).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - SSCMPackage.SOCKET__INTERFACE_TEMPLATE_INSTANTIATION, null, msgs);
			msgs = basicSetInterfaceTemplateInstantiation(newInterfaceTemplateInstantiation, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, SSCMPackage.SOCKET__INTERFACE_TEMPLATE_INSTANTIATION, newInterfaceTemplateInstantiation, newInterfaceTemplateInstantiation));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Module getModule() {
		if (module != null && module.eIsProxy()) {
			InternalEObject oldModule = (InternalEObject)module;
			module = (Module)eResolveProxy(oldModule);
			if (module != oldModule) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, SSCMPackage.SOCKET__MODULE, oldModule, module));
			}
		}
		return module;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Module basicGetModule() {
		return module;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setModule(Module newModule) {
		Module oldModule = module;
		module = newModule;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, SSCMPackage.SOCKET__MODULE, oldModule, module));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<InnerConnector> getInnerConnectors() {
		if (innerConnectors == null) {
			innerConnectors = new EObjectContainmentEList<InnerConnector>(InnerConnector.class, this, SSCMPackage.SOCKET__INNER_CONNECTORS);
		}
		return innerConnectors;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case SSCMPackage.SOCKET__INTERFACE_TEMPLATE_INSTANTIATION:
				return basicSetInterfaceTemplateInstantiation(null, msgs);
			case SSCMPackage.SOCKET__INNER_CONNECTORS:
				return ((InternalEList<?>)getInnerConnectors()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case SSCMPackage.SOCKET__INTERFACE:
				if (resolve) return getInterface();
				return basicGetInterface();
			case SSCMPackage.SOCKET__INTERFACE_TEMPLATE_INSTANTIATION:
				return getInterfaceTemplateInstantiation();
			case SSCMPackage.SOCKET__MODULE:
				if (resolve) return getModule();
				return basicGetModule();
			case SSCMPackage.SOCKET__INNER_CONNECTORS:
				return getInnerConnectors();
			case SSCMPackage.SOCKET__ENABLED:
				return isEnabled();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case SSCMPackage.SOCKET__INTERFACE:
				setInterface((Interface)newValue);
				return;
			case SSCMPackage.SOCKET__INTERFACE_TEMPLATE_INSTANTIATION:
				setInterfaceTemplateInstantiation((InterfaceTemplateInstantiation)newValue);
				return;
			case SSCMPackage.SOCKET__MODULE:
				setModule((Module)newValue);
				return;
			case SSCMPackage.SOCKET__INNER_CONNECTORS:
				getInnerConnectors().clear();
				getInnerConnectors().addAll((Collection<? extends InnerConnector>)newValue);
				return;
			case SSCMPackage.SOCKET__ENABLED:
				setEnabled((Boolean)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case SSCMPackage.SOCKET__INTERFACE:
				setInterface((Interface)null);
				return;
			case SSCMPackage.SOCKET__INTERFACE_TEMPLATE_INSTANTIATION:
				setInterfaceTemplateInstantiation((InterfaceTemplateInstantiation)null);
				return;
			case SSCMPackage.SOCKET__MODULE:
				setModule((Module)null);
				return;
			case SSCMPackage.SOCKET__INNER_CONNECTORS:
				getInnerConnectors().clear();
				return;
			case SSCMPackage.SOCKET__ENABLED:
				setEnabled(ENABLED_EDEFAULT);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case SSCMPackage.SOCKET__INTERFACE:
				return basicGetInterface() != null;
			case SSCMPackage.SOCKET__INTERFACE_TEMPLATE_INSTANTIATION:
				return interfaceTemplateInstantiation != null;
			case SSCMPackage.SOCKET__MODULE:
				return module != null;
			case SSCMPackage.SOCKET__INNER_CONNECTORS:
				return innerConnectors != null && !innerConnectors.isEmpty();
			case SSCMPackage.SOCKET__ENABLED:
				return enabled != ENABLED_EDEFAULT;
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (enabled: ");
		result.append(enabled);
		result.append(')');
		return result.toString();
	}

} //CompartmentImpl
